﻿using Orm.Entities;
using Scm.Orm.Helper;
using Scm.Service.Dto;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Scm.Service.Services
{
    public class ProjectService
    {

        /// <summary>
        /// 项目选择器方法
        /// </summary>
        /// <param name="Page"></param>
        /// <param name="User"></param>
        /// <returns></returns>
        public BaseDto<List<ProjectSelectorDto>> AllForSelector(PageDto Page)
        {
            var page = Page?.Page ?? 1;
            var size = (Page?.Size ?? 10) + 1;
            List<ScmProject> list = SqlSugarHelper.GetDB()
                .Queryable<ScmProject>()
                .Where(it => it.Deleted == 0 && it.SubscribeStart <= DateTime.Now && it.SubscribeEnd >= DateTime.Now)
                .OrderBy(it => it.CreateTime, OrderByType.Desc)
                .ToPageList(page, size);
            List<ProjectSelectorDto> Dtos = new();
            list.ForEach(it =>
            {
                var record = new ProjectSelectorDto();
                record = ObjectHandler.Copy(record, it);
                Dtos.Add(record);
            });
            if (Dtos.Count >= size)
            {
                Dtos.RemoveAt(Dtos.Count - 1);
            }
            Page ??= new PageDto();
            Page.Page = page;
            Page.Size = size - 1;
            Page.HasNext = size == (list?.Count ?? 0);
            return BaseDto<List<ProjectSelectorDto>>.Success(Dtos, Page);
        }
    }
}
